package com.orangesale.cn.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.lljjcoder.citypickerview.widget.CityPicker;
import com.orangesale.cn.R;
import com.orangesale.cn.entity.OrangeUser;
import com.orangesale.cn.netrequest.OkHttpUser;

import java.io.IOException;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private EditText usernameEdit, passwordEdit, surePasswordEdit;
    private TextView cityText;
    private CityPicker cityPicker;
    private Button regButton;
    private RadioGroup sexGroup;
    private String sexStr = "男";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_register);
        init();
    }

    /**
     * 界面组件初始化
     */
    private void init() {
        cityText = findViewById(R.id.reg_province);
        cityText.setOnClickListener(this);
        usernameEdit = findViewById(R.id.reg_username);
        passwordEdit = findViewById(R.id.reg_password);
        surePasswordEdit = findViewById(R.id.reg_sure_password);
        regButton = findViewById(R.id.reg_register);
        regButton.setOnClickListener(this);
        sexGroup = findViewById(R.id.sex);
        sexGroup.setOnCheckedChangeListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reg_province:
                initCityPicker();
                cityPicker.show();
                break;
            case R.id.reg_register:
                //注册验证方法
                //validateRegister();
                Thread thread = new Thread(runnable);
                thread.start();
                break;

        }
    }

    /**
     * 初始化城市选择器
     */
    public void initCityPicker() {
        cityPicker = new CityPicker.Builder(RegisterActivity.this)
                .textSize(16)
                .title("地址选择")
                .backgroundPop(0xa0000000)
                .titleBackgroundColor("#EFB81C")
                .titleTextColor("#000000")
                .backgroundPop(0xa0000000)
                .confirTextColor("#000000")
                .cancelTextColor("#000000")
                .province("江西省")
                .city("赣州市")
                .district("章贡区")
                .textColor(Color.parseColor("#000000"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .itemPadding(10)
                .onlyShowProvinceAndCity(false)
                .build();
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... strings) {
                String province = strings[0];
                String city = strings[1];
                String district = strings[2];
                cityText.setText(String.format("%s %s %s", province, city, district));
            }

            @Override
            public void onCancel() {
            }
        });

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        //根据用户选择来改变sex的值
        sexStr = checkedId == R.id.reg_man ? "男" : "女";
    }

    /**
     * 注册验证
     */
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            String username = usernameEdit.getText().toString();
            String password = passwordEdit.getText().toString();
            String surePassword = surePasswordEdit.getText().toString();
            String city = cityText.getText().toString();
            //判断两次密码是否输入一致
            if (password.equals(surePassword)) {
                //这里也可以再进行其它的验证，如是否符合要求等，符合就进行插入数据库操作
                if (!username.equals("") || !password.equals("")) {
                    if (!city.equals("")) {
                        Bundle bundle = new Bundle();
                        bundle.putString("username", username);
                        bundle.putString("password", password);
                        bundle.putString("sex", sexStr);
                        bundle.putString("city", city);
                        OrangeUser orangeUser = new OrangeUser();
                        orangeUser.setUsername(username);
                        orangeUser.setPassword(password);
                        orangeUser.setCity(city);
                        orangeUser.setSex(sexStr);
                        //将使用安卓自身带的SQLite数据库换成调用okhttp的方式
                        OkHttpUser okHttpUser = new OkHttpUser();
                        try {
                            if (okHttpUser.userRegister(orangeUser)) {
                                Intent intent = new Intent(RegisterActivity.this, IndexActivity.class);
                                intent.putExtras(bundle);
                                startActivity(intent);
                            } else {
                                Looper.prepare();
                                Toast.makeText(RegisterActivity.this, "注册失败！", Toast.LENGTH_SHORT).show();
                                Looper.loop();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        Looper.prepare();
                        Toast.makeText(RegisterActivity.this, "请选择地址", Toast.LENGTH_SHORT).show();
                        Looper.loop();
                    }
                } else {
                    Looper.prepare();
                    Toast.makeText(RegisterActivity.this, "账号或密码未填写", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            } else {
                Looper.prepare();
                Toast.makeText(RegisterActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }
    };
}
